<template>
  <div>
    <div class="goodsindex">
      <!-- 搜索条件 -->
      <el-form ref="form" :model="queryInfo" >
        <el-row :gutter="20" class="goodsindex-queryInfo">
          <!-- 商品名称搜索 -->
          <el-col :xs="8" :sm="6" :md="6" :lg="4" :xl="4">
            <el-select  class="goodsindex-queryInfo-li" v-model="queryInfo.role_id" filterable size="small" clearable placeholder="请选择角色">
              <el-option
                  v-for="item in role_name"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
              </el-option>
            </el-select>
          </el-col>
          <el-col :xs="6" :sm="4" :md="3" :lg="2" :xl="2">
            <el-button type="primary" class="goodsindex-queryInfo-li" size="small"  @click="search()">搜索</el-button>
          </el-col>
        </el-row>
      </el-form>
      <!-- 检索结果 -->
      <el-row :gutter="20" class="goodsindex-list">
        <el-col :span="24">
          <el-table
              :data="tableData"
              border
              size='small'
              style="width: 100%">
            <el-table-column
                prop="role_id"
                label="角色id"
                width="80">
            </el-table-column>

            <el-table-column
                prop="role_name"
                label="角色名称"
                width="120">
            </el-table-column>

            </el-table-column>
            <el-table-column
                fixed="right"
                label="操作"
            >
              <template  v-slot:default="scope">
                <el-button type="text" size="small" @click="editForm(scope.$index,scope.row)">编辑</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-col>
      </el-row>
      <!-- 分页 -->
      <el-row :gutter="20" class="goodsindex-list">
        <el-col :span="24" class="goodsindex-page-box">
          <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="queryInfo.page"
              :page-sizes="[10, 20, 50, 100]"
              :page-size="queryInfo.pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="queryInfo.total">
          </el-pagination>
        </el-col>
      </el-row>
    </div>

    <RoleMenu :visible-child.sync="visible" :child-form="RoleForm" :table-tree="TableTree"></RoleMenu>
  </div>

</template>


<script>
import {getBaseRoles, roleAuth, menuAll} from "@/api/api.js";
import RoleMenu from "@/views/auths/RoleMenu.vue";
export default {
  name: "RoelAuth",
  data(){
    return {
      visible:false,
      role_name:[],
      queryInfo:{
        role_id:'',
        page: 1 ,
        pageSize: 10,
        total:10
      },
      tableData: [],
      // 这是给子组件传递的参数
      RoleForm:{
        role_id : -1,
        role_name:'',
      },
      TableTree:[
        //   这里是传递给新增组件和修改组件的，避免出现点击新增和修改时候还要请求一次后台
      ],
    }
  },
  components:{
    RoleMenu,
  },
  methods:{
    handleSizeChange(pageSize){
      // console.log('1',pageSize);
      this.roles([],this.queryInfo.page,pageSize);
    },
    handleCurrentChange(page){
      // console.log('2',page);
      this.roles([],page,this.queryInfo.pageSize);
    },
    editForm(index,value){
      this.visible = true;
      this.RoleForm = value;

    },
    getBaseRoles(){
      getBaseRoles({ is_delete: 0 }).then((res) =>{
        if (res.code === 200){
          let options = [
            {
              label: '',
              value: ''
            }
          ];
          this.$.each(res.data,(index,values)=>{
            options.push({
              label: values.role_id + '--' + values.role_name,
              value:values.role_id
            })
          })
          this.role_name = options;
        }
      }).catch(error =>{
        this.$message({
          type:'error',
          message:error.message || 'Error'
        });
      })
    },
    roles(data,page,pageSize){
      roleAuth(data,page,pageSize).then((res) =>{
        if (res.code === 200){
          // 这个分页的三条数据是为了防止分页器添加或者修改数据之后产生的bug,尤其是选择了多个分页条数之后
          this.queryInfo.total = res.data.total;  //总和
          this.queryInfo.page  = res.data.current_page; // current_page	当前页码
          this.queryInfo.pageSize  = res.data.per_page;// per_page	每页条数
          this.tableData = res.data.data;
          // console.log("this.tableData",this.tableData);
        }
      }).catch(error =>{
        this.$message({
          type:'error',
          message:error.message || 'Error'
        });
      })
    },
    search(){
      this.roles(this.queryInfo,this.queryInfo.page,this.queryInfo.pageSize);
    },
    //查询菜单所有数据
    menu(data = null){
      // console.log('data',data);
      menuAll(data).then((res) =>{
        if (res.code === 200){
          // 这里是因为后端进行树循环，就不在前端进行判断了，避免浪费，同时也避免出现循环一层的情况出现
          this.TableTree = this.buildOptions(res.data);
        }
      }).catch(error =>{
        this.$message({
          type:'error',
          message:error.message || 'Error'
        });
      })
    },
    buildOptions(menuList) {
      // 树形无限极循环
      let options = [];
      for (let i = 0; i < menuList.length; i++) {
        let menu = menuList[i];
        let option = {
          id: menu.menu_id,
          label: menu.menu_name
        };
        if (menu.children && menu.children.length > 0) {
          option.children = this.buildOptions(menu.children);
        }
        options.push(option);
      }
      return options;
    },
  },
  created() {
    this.roles([],this.queryInfo.page,this.queryInfo.pageSize);
    this.getBaseRoles();
    this.menu();
  }
}
</script>


<style scoped>

</style>